from django.contrib.auth.models import AbstractUser
from django.db import models

# Create your models here.
from ERP_system.utils.basemodels import BaseModel

#测试模型类
class Snippet(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=100, blank=True, default='')
    code = models.TextField()
    linenos = models.BooleanField(default=False)
    language = models.CharField( default='python', max_length=100)
    style = models.CharField(default='friendly', max_length=100)

    class Meta:
        ordering = ['created']

        db_table='snippet'
        verbose_name='代码段'

    def __str__(self):
         return  self.code


# 菜单功能的模型类
class Menu(BaseModel):
    number = models.IntegerField('排序数字', blank=True, null=True)
    url = models.CharField('菜单访问的URL', max_length=256, blank=True, null=True)
    delete_flag = models.CharField('是否删除', max_length=1, default='0')
    name = models.CharField('菜单名字', max_length=50)
    parent = models.ForeignKey('self', blank=True, null=True, related_name='children', on_delete=models.CASCADE)
    class Meta:
        db_table = 't_menu'
        verbose_name = '功能菜单'
        verbose_name_plural = verbose_name
        def __str__(self): return self.name

class UserModel(AbstractUser):

    phone = models.CharField('手机号',max_length=128,null=True,blank=True,unique=True)
    real_name = models.CharField('真实姓名',max_length=128,blank=True,null=True)
    roles=models.ManyToManyField('RolesModel',db_table='user_to_roles',blank=True,null=True,verbose_name='角色')
    dept=models.ForeignKey('DeptModel',max_length=128,null=True,blank=True,on_delete=models.SET_NULL,
                           verbose_name='用户所在部门')

    class Meta:
        db_table='t_user'
        verbose_name='用户表'
        verbose_name_plural=verbose_name


    def __str__(self):
        return  self.username

class PermissionsModel(BaseModel):

    '''

    权限模型类
    '''

    method_choices=(
        ('POST','增'),
        ('DELETE','删'),
        ('PUT','改'),
        ('PATCH','局部修改'),
        ('GET','查')
    )
    name = models.CharField(max_length=50, verbose_name='权限名')
    is_menu = models.BooleanField(verbose_name='是否为菜单',default=True)  # True为菜单,False为接口
    # 操作
    method = models.CharField(max_length=8, blank=True, default='', choices=method_choices, verbose_name='请求方法')
    path = models.CharField(max_length=200, blank=True, default='', verbose_name='请求路径')
    # 资源
    menu = models.ForeignKey('Menu', null=True, blank=True, related_name='permissions_list', on_delete=models.CASCADE)
    desc = models.CharField(max_length=512, blank=True, default='', verbose_name='权限描述')

    def __str__(self):
        return self.name


    class Meta:
        db_table='t_permissions'
        verbose_name='权限'
        verbose_name_plural=verbose_name

        ordering=['id']


class RolesModel(BaseModel):
    name = models.CharField(max_length=32, unique=True, verbose_name='角色名字')
    permissions = models.ManyToManyField('PermissionsModel', db_table='t_roles_permissions', blank=True,
                                         verbose_name='权限')

    def __str__(self):
        return self.name

    class Meta:
        db_table = 't_roles'
        verbose_name = '角色'
        verbose_name_plural = verbose_name


class DeptModel(BaseModel):
    name = models.CharField(max_length=32, unique=True, verbose_name='部门名字')
    adress=models.CharField(max_length=256,null=True,blank=True,verbose_name='部门地址')
    parent=models.ForeignKey('self',max_length=25,null=True,blank=True,related_name='children',on_delete=models.CASCADE,
                             verbose_name='部门所在父部门')


    def __str__(self):
        return self.name

    class Meta:
        db_table = 't_dept'
        verbose_name = '部门'
        verbose_name_plural = verbose_name
